<template>
  <div class="home">
    <banner isHome="true"></banner>
    <div class="site-content animate">
      <!--通知栏-->
      <div class="notify">
        <div class="search-result" v-if="hideSlogan">
          <span v-if="searchWords">搜索结果："{{searchWords}}" 相关文章</span>
          <span v-else-if="category">分类 "{{categoryName}}" 相关文章</span>
          <span v-else-if="tag">标签 "{{tagName}}" 相关文章</span>
        </div>
        <quote v-else>{{notice}}</quote>
      </div>

      <!--焦点图-->
      <div class="top-feature" v-if="!hideSlogan">
        <section-title>
          <div style="display: flex;align-items: flex-end;">聚焦<small-ico></small-ico>
          </div>
        </section-title>
        <div class="feature-content">
          <div class="feature-item" v-for="item in features" :key="item.title">
            <Feature :data="item"></Feature>
          </div>
        </div>
      </div>
      <!--文章列表-->
      <main class="site-main" :class="{'search':hideSlogan}">
        <section-title v-if="!hideSlogan"><a href="javascript:void(0);">最新</a>/<a style="margin-left:20px" href="javascript:void(0);">最热</a></section-title>
        <template v-for="item in postList">
          <post :post="item" :hideInfoMeta=hideInfoMeta :key="item.id"></post>
        </template>
      </main>

      <!--加载更多-->
      <div class="more" v-show="hasNextPage">
        <div class="more-btn" @click="loadMore">
          <i class="el-icon-loading" v-show="moreLoading"></i>
          <span v-show="!moreLoading">More</span>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import Banner from '@/components/banner'
import Feature from '@/components/feature'
import sectionTitle from '@/components/section-title'
import Post from '@/components/post'
import SmallIco from '@/components/small-ico'
import Quote from '@/components/quote'
import { fetchFocus, fetchList, getNewBlog, getBlogByLevel, searchBlog } from '../api'
import { categoryArticle } from '../api/category'
import { tagArticle } from '../api/tag'
// vuex中有mapState方法，相当于我们能够使用它的getset方法
import { mapMutations } from 'vuex';
export default {
  name: 'Home',
  props: ['cate', 'words'],
  data() {
    return {
      moreLoading: false,
      features: [],
      postList: [],
      pageSize: 10,
      currentPage: 1,
      hasNextPage: false,
      topReq: {
        "index": 0,
        "size": 3,
        "ascOrDesc": "DESC",
        "queryDto":{
          "online": 1,
          "isTop": 1
        },
        "properties": ["is_top","update_time","is_essence","comment_count","read_count"]
      },
      indexParam: {
        "index": 0,
        "size": 10,
        "ascOrDesc": "DESC",
        "queryDto":{
          "online": 1
        },
        "properties": ["is_essence","update_time","comment_count","read_count"]
      }
    }
  },
  components: {
    Banner,
    Feature,
    sectionTitle,
    Post,
    SmallIco,
    Quote
  },
  computed: {
    searchWords() {
      return this.$route.params.words
    },
    category() {
      return this.$route.params.typeId
    },
    categoryName() {
      return this.$route.params.typeName
    },
    tagName() {
      return this.$route.params.tagName
    },
    tag() {
      return this.$route.params.tagId
    },
    hideSlogan() {
      return this.category || this.searchWords || this.tag
    },
    notice() {
      return this.$store.getters.webConfigData.banner;
    },
    //隐藏搜索结果页的infoMeta
    hideInfoMeta() {
      return this.$route.params.words != undefined || this.$route.params.cate!=undefined || this.$route.params.tag!=undefined;
    }
  },
  mounted() {
    this.$store.dispatch('setFixed', false);
    window.addEventListener('scroll', this.watchScroll)
    this.init()
    this.getArtivleList()
  },
  beforeDestroy() {
    this.$store.dispatch('setFixed', true);
    window.removeEventListener("scroll", this.watchScroll)
  },
  methods: {
    init() {
      this.topReq.queryDto.blogType = this.category
      this.indexParam.queryDto.blogType = this.category
      this.topReq.queryDto.tagId = this.tag
      this.indexParam.queryDto.tagId = this.tag
    },
    getArtivleList() {
      this.$store.dispatch('setLoading', true)
      this.fetchFocus();
      this.fetchList();
    },
    watchScroll() {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      if ((scrollTop === 0 || scrollTop < 150)) {
        this.$store.dispatch('setFixed', false);
      } else {
        this.$store.dispatch('setFixed', true);
      }
    },
    // 置顶博客
    fetchFocus() {
      var params = this.topReq;
      getBlogByLevel(params).then(res => {
        this.features = res.data.data.result || []
      })
    },
    // 时间排序
    fetchList() {
      var params = this.indexParam;
      params.index = this.currentPage;
      params.size = this.pageSize;
      getNewBlog(params).then(res => {
        if(res.data.code !== 200) {
          alert(res.data.msg)
          return
        }
        if (this.moreLoading) {
          this.postList = this.postList.concat(res.data.data.result || [])
        } else {
          this.postList = res.data.data.result || []
        }
        this.hasNextPage = res.data.data.pages > this.currentPage;
        this.$store.dispatch('setLoading', false);
        this.moreLoading = false;
      })
    },
    loadMore() {
      this.moreLoading = true;
      this.currentPage = this.currentPage + 1;
      this.getArtivleList();
    },
    // loadMore() {
    //   this.moreLoading = true;
    //   if (this.searchWords == null) {
    //     this.currentPage = this.currentPage + 1;
    //     var params = new URLSearchParams();
    //     params.append("pageSize", this.pageSize);
    //     params.append("currentPage", this.currentPage);
    //     getNewBlog(params).then(res => {
    //       this.postList = this.postList.concat(res.data.records || [])
    //       this.currentPage = res.data.current
    //       this.hasNextPage = Math.ceil(res.data.total / res.data.size) > this.currentPage;
    //       this.$store.dispatch('setLoading', false);
    //       this.moreLoading = false;
    //     })
    //   } else {
    //     this.currentPage = this.currentPage + 1;
    //     var params = new URLSearchParams();
    //     params.append("currentPage", this.currentPage);
    //     params.append("pageSize", this.pageSize);
    //     params.append("keywords", this.searchWords);
    //     searchBlog(params).then(res => {
    //       this.postList = this.postList.concat(res.data.esBlogIndexList || [])
    //       this.hasNextPage = Math.ceil(res.data.total / res.data.size) > this.currentPage;
    //       this.$store.dispatch('setLoading', false);
    //     })
    //   }
    // },

  }

}
</script>
<style scoped lang="less">
.site-content {
  .notify {
    margin: 40px 0;
    border-radius: 3px;
    & > div {
      padding: 20px;
    }
  }

  .search-result {
    padding: 15px 20px;
    text-align: center;
    font-size: 20px;
    font-weight: 400;
    border: 1px dashed #ddd;
    color: #828282;
  }
}

.top-feature {
  width: 100%;
  height: auto;
  margin-top: 30px;

  .feature-content {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    position: relative;

    .feature-item {
      width: 32.9%;
    }
  }
}

.site-main {
  padding-top: 40px;

  &.search {
    padding-top: 0;
  }

  a {
    margin-right: 20px;
  }
}

.more {
  margin: 50px 0;
  .more-btn {
    width: 100px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #adadad;
    border: 1px solid #adadad;
    border-radius: 20px;
    margin: 0 auto;
    cursor: pointer;
    &:hover {
      color: #8fd0cc;
      border: 1px dashed #8fd0cc;
    }
  }
}

/******/
@media (max-width: 800px) {
  .top-feature {
    display: none;
  }

  .site-main {
    padding-top: 40px;
  }

  .site-content {
    .notify {
      margin: 30px 0 0 0;
    }

    .search-result {
      margin-bottom: 20px;
      font-size: 16px;
    }
  }
}

/******/
</style>
